function p = mcPi(n, show)
% 蒙特卡洛估算圆周率
% 输入: 
%     n 样本点个数
%     show 是否显示图示
% 输出: p 估算圆周率

x = rand(n, 2);
y = sum((x - 1) .^ 2, 2);
z = (y <= 1);
p = sum(z) / n * 4;

if nargin < 2
    show = 1;
end
if show
    figure; hold on
    x1 = x .* [z, z];
    x2 = x .* [(y > 1), (y > 1)];
    scatter(x1(:, 1), x1(:, 2), 'r', '.');
    scatter(x2(:, 1), x2(:, 2), 'b', '.');
end

end